<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//列表页控制器
class ListController extends Controller{
	private $goods;
	//构造函数
	public function __init(){
		//实例化商品表
		$this->goods = new \Common\Model\Goods;	
	}
	//展示列表页
	public function index(){
		 $cid = Q('get.cid');
		 //查询顶级分类数据
         $cateData = Db::table('category')->where("pid = 0")->get();
		 View::with('cateData',$cateData);
		 
		//根据传过来的cid查找父级cid
		$fcids = $this->getFatherCids(Db::table('category')->get(),Q('get.cid',0,'intval'));
		//把自己压入数组
		array_push($fcids,$cid);
		 //根据得到的$fcids查找所有的父级分类名称(包括自己的名字)
		$fatherCate = Db::table('category')->where("cid in (" . implode(',', $fcids) . ")")->get();
		View::with('fatherCate',$fatherCate);
		 //1.根据当前的cid获得对应商品的gid
		 $gids = $this->cidGetGids(Q('get.cid'),0,'intval');
		 
		 //2.根据gid找到商品属性
		 $goodsAttr = $this->gidGetAttr($gids);
		 View::with('goodsAttr',$goodsAttr);
		 
	  	 //3.查询品牌
	     $brand = M("\Common\Model\Brand")->get();
		 View::with('brand',$brand);
		 
		 //4.处理筛选参数param 0_0_.....
		 $num = count($goodsAttr);
		 $param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
		 View::with('param',$param);
		 
		 //5.根据gids查询商品表
		 $goods = $this->goods->where("gid in (" . implode(',', $gids) . ")")->get();
		 View::with('goods',$goods);
		 //载入模板
	     View::make();	  
	}
	
	//过滤
	private function filter($param,$cidGids){
		$gids = array();
		foreach ($param as $gaid) {
			if($gaid){
				//1.找到gvalues,比如'蓝色'
				$gvalues = Db::table('goodsattr')->where("gaid = {$gaid}")->pluck('gvalues');
				//2.再通过'蓝色'查找到对应的商品的gid
				$gids[] = Db::table('goodsattr')->where("gvalues = '{$gvalues}'")->lists('gid');
			}
		}
		//如果点了'不限'之外的属性
		if($gids){
			//去交集
			$gidsTemp = $gids[0];
			foreach ($gids as $v) {
				$gidsTemp = array_intersect($gidsTemp, $v);
			}
			//和当前分类对应的gid再次取交集
			return array_intersect($gidsTemp, $cidGids);
		}else{//如果全部是'不限的时候'
			return $cidGids;
		}
	}
	
	//通过gid获得商品属性
	private function gidGetAttr($gids){
		//查询商品属性表获得$gids对应的所有商品属性值
		$data = Db::table('goodsattr')->where("goods_gid in (" . implode(',', $gids) . ")")->groupBy('gvalues')->get();
		//1.把相同类型属性的id归结在一起
		//p($data);
		$sameAttr = array();
		foreach ($data as $v) {
			$sameAttr[$v['typeattr_tid']][] = $v;
		}
		//2.组合数组
		$temp = array();
		foreach ($sameAttr as $typeattr_tid => $v) {
			$temp[] = array(
				'name' => Db::table('typeattr')->where("tid={$typeattr_tid}")->pluck('tname'),
				'value'=>$v
			);
		}
		return $temp;
		
	}
	
	//通过cid获得gid
	private function cidGetGids($cid){
		//1.通过cid获得对应的所有子集cid
		$cids = $this->getSonCids(Db::table('category')->get(), $cid);
		//再把自己压进去
		$cids[] = $cid;
		//2.通过自己cid查找对应的商品的gid
		$gids = Db::table('goods')->where("category_cid in (" . implode(',', $cids) . ")")->lists('gid');
		return $gids;
		
	}
	
	//递归获得指定cid的所有子集
	private function getSonCids($data,$cid){
		$temp = array();
		foreach ($data as $k => $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->getSonCids($data, $v['cid']));
			}
		}
		return $temp;
	}
	
	 //根据传过来的cid递归找所有父级分类	 
	 private function getFatherCids($data,$cid){	 		
		//根据传过来的当前商品的cid查询pid
		$pid = Db::table('category')->where("cid = {$cid}")->pluck('pid');
	 	$temps = array();
		foreach ($data as $k => $v) {
			if($v['cid'] == $pid){
				$temps[] = $v['cid'];
				$temps = array_merge($temps,$this->getFatherCids($data, $v['cid']));
			}
		}
		return $temps;
	 }
	
	
}




?>